package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: MeberServiceImpl
 * @Author: WangZhe
 * @Date: 2020/1/1 18:11
 * @Description: //TODO
 */

@Service(interfaceClass = MemberService.class)
@Transactional
public class MeberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            //使用MD5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);

    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> month){
        List<Integer> list = new ArrayList<>();
        for(String m : month){
            m = m + ".31";//格式:2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return  list;
    }

    @Override
    public List<Map<String, Object>> findMemberSexCount() {
        return memberDao.findMemberSexCount();
    }

    @Override
    public List<Map<String, Object>> findMemberAgeCount() {
        return memberDao.findMemberAgeCount();
    }
}
